package com.frame.pageform;

import java.lang.reflect.*;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ResponseUtils {

	private static Method encode = null;
	private final static String regxpForHtml = "<([^>]*)>"; // 过滤所有以<开头以>结尾的标签

	public static String encodeURL(String url) {
		return encodeURL(url, "UTF-8");
	}
	public static String filter(String value) {
		if(value != null)
		{
			value = value.replaceAll("&","&amp;");
			value = value.replaceAll("\"","&quot;");
			value = value.replaceAll("'","&#39;");
			value = value.replaceAll("<","&lt;");
			value = value.replaceAll(">","&gt;");
		}
		return value;
	}
	public static String clearQuot(String value){
		if(value != null)
		{
			value = value.replaceAll("\"","");
			value = value.replaceAll("'","");
		}
		return value;
	}
	public static String clearHtml(String str) {
		Pattern pattern = Pattern.compile(regxpForHtml);
		Matcher matcher = pattern.matcher(str);
		StringBuffer sb = new StringBuffer();
		boolean result1 = matcher.find();
		while (result1) {
			matcher.appendReplacement(sb, "");
			result1 = matcher.find();
		}
		matcher.appendTail(sb);
		return sb.toString();
	}
	public static String encodeURL(String url, String enc) {
		try {
			if ((enc == null) || (enc.length() == 0)) {
				enc = "UTF-8";
			}

			// encode url with new 1.4 method and UTF-8 encoding
			if (encode != null) {
				return (String) encode.invoke(null, new Object[] { url, enc });
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}

		return URLEncoder.encode(url);
	}
}
